//
//  ViewController.m
//  hexa
//
//  Created by kelly on 2014. 12. 31..
//  Copyright (c) 2014년 campmobile. All rights reserved.
//

#import "ViewController.h"
#define ONE_SECOND (1)

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    [_boardView setupViews:self];
    [_blockView random];
    [self place];
    [_boardView flow];
    //[self startTimer];
}

- (void)startTimer
{
    timer = [NSTimer timerWithTimeInterval:ONE_SECOND
                                    target:self
                                  selector:@selector(onTimer:)
                                  userInfo:nil
                                   repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
}

- (void)end
{
    [self place];
}

- (void)place
{
    BOOL result = [_boardView place:_blockView.firstBlock.tag second:_blockView.secondBlock.tag third:_blockView.thirdBlock.tag];
    if (!result) {
        //[self stopTimer];
    } else {
        [_blockView random];
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (IBAction)clickChangeButton:(id)sender
{
    [_boardView change];
}

- (IBAction)clickRightButton:(id)sender
{
    [_boardView right];
}

- (IBAction)clickLeftButton:(id)sender
{
    [_boardView left];
}

- (IBAction)clickDownButton:(id)sender
{
    [_boardView down];
}

- (void)onTimer:(NSTimer *)theTimer
{
    /*if (![_boardView flow]) {
     [self place];
     }*/
}

- (void)stopTimer
{
    if ([timer isValid]) {
        [timer invalidate];
    }
    timer = nil;
}

@end
